// pages/publish/publish.js
const {
  post
} = require('../../utils/http')

Page({

  /**
   * 页面的初始数据
   */
  //address，latitude，longitude，message，
  //contact，type（sell，buy），openid（可不传）
  data: {
    latitude: '',
    longitude: '',
    address: '点击选择',
    type: 'buy',
    message: '',
    contact: '',
    issubmit: false,
    isresult:false,
    issuccess: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  chooseLocation() {
    wx.chooseLocation({
      complete: (res) => {
        console.log(res)
        this.setData({
          address: res.address,
          longitude: res.longitude,
          latitude: res.latitude
        })
      }
    })
  },
  radioChange(e) {
    console.log(e);
    this.setData({
      type: e.detail.value
    })
  },

  inpuntMessage(e) {
    this.setData({
      message: e.detail.value
    })
  },
  inputContact(e) {
    this.setData({
      contact: e.detail.value
    })
  },

  async handleSubmit() {

    var options = {
      icon: 'none',
      duration: 2000
    }

    if (this.data.address === '点击选择') {
      wx.showToast({
        title: '请选择地址信息',
        ...options
      })
      return;
    }


    if (!this.data.message) {
      wx.showToast({
        title: '请填写您的具体需求',
        ...options
      })
      return;
    }

    if (!this.data.contact) {
      wx.showToast({
        title: '请填写您的联系方式',
        ...options
      })
      return;
    }

    this.setData({
      issubmit: true
    })
    wx.showLoading({
      title: 'loading...',
    })

    let rs = await post('/index.php/trade/add_item', {
      address: this.data.address,
      latitude: this.data.latitude,
      longitude: this.data.longitude,
      message: this.data.message,
      contact: this.data.contact,
      type: this.data.type
    })
    console.log(rs)

    wx.hideLoading({
      success: (res) => {}
    })

    this.setData({
      isresult:true,
      issuccess:rs.ret
    })





  },

  // goHome() {
  //   wx.navigateTo({
  //     url: '/pages/index/index',
  //   })
  // },
  republish(){
    this.setData({
      issubmit:false,
      issuccess:false,
      isresult:false
    })
  }


})